﻿using Entity.Customer;
using Entity.ResultModel.Customer;
using IHotelBLL.ICustomerBLL;
using IHotelDAL.ICustomerDAl;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HotelBLL.CustomerBLL
{
    public class CustomerBLL:ICustomerBLL
    {
        ICustomerDAL _customerDAL;
        public CustomerBLL(ICustomerDAL customerDAL)
        {
            _customerDAL = customerDAL;
        }
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetCustomerInfoModel> GetCustomer(int page, int limit, string name, out int count)
        {
            var datas = _customerDAL.GetEntities();
           
            count = datas.Count();
           
            var roomInfos = datas.Skip((page - 1) * limit).Take(limit).Select(f => new

            {
                f.Id,
                f.Name,
                f.Sex,
                f.PassWord,
                f.Phone,
                f.Address,
                f.CreateTime,
                f.ID_Card,
                f.IsVip
                
            }).ToList();
            var tempList = roomInfos.Select(f=>
            {
                //string sexStr = f.Sex == 0 ? "女" : "男";
                string dateTimeStr = f.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
                return new GetCustomerInfoModel
                {

                    Id = f.Id,
                    Name = f.Name,
                    Address = f.Address,
                    Phone = f.Phone,
                    Sex = f.Sex,
                    IsVip = f.IsVip,
                    CreateTime = dateTimeStr
                    
                };
            }).ToList();

            return tempList;



        }

        /// <summary>
        /// 修改更新顾客
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="sex"></param>
        /// <param name="phone"></param>
        /// <param name="address"></param>
        /// <param name="customerAddress"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdateCustomerInfo(string id, string name, string sex, string phone, string address, out string msg)
        {
            CustomerInfo customerInfo = GetCustomerInfoById(id);
            if (customerInfo == null)
            {
                msg = "未查询到需要修改的用户";
                return false;
            }

            customerInfo.Name = name;
            customerInfo.Sex = sex;
            customerInfo.Phone = phone;
            customerInfo.Address = address;

            bool isSuccess = _customerDAL.UpdateEntity(customerInfo);
            if (isSuccess)
            {
                msg = "修改成功";
                return true;
            }
            else
            {
                msg = "修改失败";
                return true;
            }
            
        }

        /// <summary>
        /// 根据id查询用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public CustomerInfo GetCustomerInfoById(string id)
        {
            return _customerDAL.GetEntities().FirstOrDefault(u => u.Id == id);
        }

        public bool AddCustomer(string name, string sex, string iD_Card, string phone, out string msg)
        {
            CustomerInfo customerInfo = new CustomerInfo()
            {
                Id = Guid.NewGuid().ToString(),
                Name = name,
                Phone  = phone,
                Sex = sex,
                ID_Card = iD_Card
            };
            bool isSuccess = _customerDAL.CreateEntity(customerInfo);
            msg = isSuccess ? "添加成功" : "添加失败";
            return isSuccess;
        }
    }
}
